//
//  Trip.swift
//  ImGuider X
//
//  Created by 陆涛 on 2018/9/28.
//  Copyright © 2018 imguider. All rights reserved.
//

import UIKit
import CoreLocation

/// 行程类型
///
/// - other: 其他
/// - flight: 航班
/// - hotel: 酒店
/// - scenic: 景点
/// - restaurant: 餐饮
/// - shopping: 购物
/// - activity: 活动
/// - line: 线路
enum PlanType:Int,Codable {
    
    case flight = 1
    case hotel = 2
    case scenic = 3
    case restaurant = 4
    case shopping = 5
    case activity = 6
    case other = 7
    case line = 8
}


class Promotion: NSObject,Codable {
    
    var discountNum,name,detail,type,url,useTip,remarks:String?
    var moneyType,freeNum:String?
}

class Calendar: NSObject,Codable {
    
    var id:String?
    var planType:PlanType?
    var planTime,intro,address,title,etitle,telephone,website:String?
    var pictures,opentime,tags,consumption:String?
    var daty,night:Int?
    var lat,lng:Double?
    
    var viewDetail:Scenic?
    var lineDetail:Line?
    
    var airplaneDetail:AirplaneDetailModel?
    var promotion:[Promotion]?
    
    var checkinDate,checkoutDate:String?// 酒店入住日期
    var facilities:[[String:String]]?// 酒店设施
    
    var pictureArray:[String]? {
        
        return pictures?.components(separatedBy: ",").compactMap({ str -> String? in
            
            return str.count > 0 ? str : nil
        })
    }
    
    var tagArray:[String]? {
        
        return tags?.components(separatedBy: ",").compactMap({ str -> String? in
            
            return str.count > 0 ? str : nil
        })
    }
    
    var coor:CLLocationCoordinate2D? {
        
        if let viewdetail = viewDetail {
  
            return viewdetail.coor
        }
        
        if let line = lineDetail {
            
            if line.records?.count ?? 0 >= 2 {
                
                return line.records?[1].coor
            }
        }
    
        guard let lat = lat,let lng = lng else {
            
            return nil
        }
        
        let coor = CLLocationCoordinate2D(latitude: lat, longitude: lng)
        
        if CLLocationCoordinate2DIsValid(coor) {
            
            return coor
        }
        
        return nil
    }
}

class Day: NSObject,Codable {
    
    var date: String?
    var calendars: [Calendar]?
    var hotel: Calendar?
    var dayofweek: Int?
    var day:Int?
    var inCity : String?
    var pictureArray:[String]? {
        
        guard let calendars = calendars else {
            
            return nil
        }
        
        let images = calendars.reduce([]) { (array, calendar) -> [String] in
            
            if let pictureArray = calendar.pictureArray, pictureArray.count > 0 {
                
                return array + pictureArray
            }
            
            return array
        }
        
        if images.count > 0 {
            
            return images
        }
        
        return nil
    }
    
    var eventInfo: [String]? {
        
        return calendars?.compactMap({ calendar -> String? in
            
            
            return calendar.title
        })
    }
    
    var featureCalendar:[Calendar]? {
        
        return calendars?.filter({ cal -> Bool in
            
            if let _ = cal.coor, cal.pictureArray?.count ?? 0 > 0 {

                return true
            }
            
            return false
        })
    }
}

class City: NSObject,Codable {
    
    var cityName,pic:String?
    var destinationDate:String?
    var stay:Int?
    
    var hotels:[Calendar]?
    var days:[Day]?
    
    
    /// 特色事件
    var featureCalendar: [Calendar]? {
        
        let array = days?.reduce([], { (array, day) -> [Calendar] in
            
            if let calendars = day.calendars?.filter({ calendar -> Bool in
                
                guard let planType = calendar.planType else { return false }
                
                if planType == .scenic || planType == .activity || planType == .line {
                    
                    return true
                }
                
                return false
            }) {
                
                return array + calendars
            }
            
            return array
        })
        
        var newArray:[Calendar] = []
        
        array?.forEach({ calendar in
            
            if !newArray.contains(where: { other -> Bool in
                
                if let id = calendar.viewDetail?.id, let otherid = other.viewDetail?.id, id == otherid {
                    
                    return true
                }
                
                return false
            }) {
                
                newArray.append(calendar)
            }
            
        })
        
        return newArray
    }
}


/// 广告类型
///
/// - website: 跳转网页
/// - trip: 跳转行程
/// - citytour: 城市导览
enum TripADType:Int, Codable {
    
    case website = 1
    case trip = 2
    case citytour = 3
}

class TripAD: NSObject,Codable {
    
    var adType:TripADType?
    var adPic:String?
    var adDetail:String?
    var orderId:String?
    var adTitle:String?
    
    var lineDetail:Calendar?
    
}

class Trip: NSObject,Codable {
    
    var orderId:String?
    var maps,pictures:String?
    var returnDate,departureDate:String?
    var planName,tags,createTime,intro,days:String?
    
    var cities:[City]?
    var flights:[AirplaneDetailModel]?
    
    var advertisements:[TripAD]?
    
    var pictureArray:[String]? {
        
        return pictures?.components(separatedBy: ",").compactMap({ str -> String? in
            
            return str.count > 0 ? str : nil
        })
    }
    
    var mapArray:[String]? {
        
        return maps?.components(separatedBy: ",").compactMap({ str -> String? in
            
            return str.count > 0 ? str : nil
        })
    }
    
    func planDate(_ format:String) ->String? {
        
        if let departureDate = departureDate, let stay = days, let time = TimeInterval(stay) {
            
            let date = Date.date(departureDate
                , "yyy-MM-dd");
            let returnDate = date?.addingTimeInterval(time * 24 * 3600)
            
            return "\(date?.string(format) ?? "") - \(returnDate?.string(format) ?? "")"
        } else {
            
            return nil
        }
    }
}




